package com.zhao.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhao.bean.Orders;
import com.zhao.bean.PageParam;
import com.zhao.bean.R;
import com.zhao.dto.OrdersDto;
import com.zhao.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.ParseException;

/**
 * @author HK
 */

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrdersService ordersService;

    public R<String> submit(@RequestBody Orders orders , HttpSession session){
        log.info("订单数据:{} " , orders);
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    //历史订单
    @GetMapping("/userPage")
    public R selectpage(int page,int pageSize){
        IPage<OrdersDto> selectpage = ordersService.selectpage(page, pageSize);
        try {
            return R.success(selectpage);
        } catch (Exception e) {
            e.printStackTrace();
            return   R.error("查询失败");
        }
    }

    /**
     * 订单分页查询(后台)
     *
     * @return
     */
    @GetMapping("/order/page")
    public R findPage(PageParam pageParam) throws ParseException {
        System.out.println("pageParam = " + pageParam);


        IPage<Orders> page = ordersService.findPage(pageParam);
        return R.success(page);
    }

}
